/**
 *
 */
package jdk8;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * @Title:
 * @author zhoujie
 * @date 2019年2月13日 上午9:26:27
 *
 */
public class CollectorsDomo {
    /**
     * 通过 mapping改变映射
     */
    @Test
    public void test01() {
        List<User> users = Arrays.asList(new User(1, "sky"), new User(1, "张三"), new User(3, "李四"));
        Map<Integer, Set<String>> userMap = users.stream().collect(Collectors.groupingBy(User::getId, Collectors.mapping(User::getName, Collectors.toSet())));
        System.out.println(userMap);
    }

    @Test
    public void test02() {
        List<User> users = Arrays.asList(new User(1, "sky"), new User(1, "张三"), new User(3, "李四"));
        Set<String> names = users.stream().collect(Collectors.mapping(User::getName, Collectors.toSet()));
        //Set<String> names2 = users.stream().map(User::getName).collect(Collectors.toSet());
        System.out.println(names);
    }


    @Test
    public void test03() {
        List<User> users = Arrays.asList(new User(1, "sky"), new User(1, "张三"), new User(3, "李四"));
        Long conut = users.stream().collect(Collectors.counting());
        System.out.println(conut);
    }

    @Test
    public void test04() {
        List<User> users = Arrays.asList(new User(1, 18, "sky"), new User(1, 19, "张三"), new User(3, 20, "李四"));
        Optional<Integer> maxId = users.stream().map(User::getId).collect(Collectors.reducing(BinaryOperator.maxBy(Integer::compareTo)));
        System.out.println(maxId.get());
        Integer sum = users.stream().collect(Collectors.reducing(0, User::getAge, Integer::sum));
        System.out.println(sum);
    }

    @Test
    public void test05() {
        List<User> users = Arrays.asList(new User(1, 18, "sky"), new User(1, 19, "张三"), new User(3, 20, "李四"));
        IntSummaryStatistics collect = users.stream().collect(Collectors.summarizingInt(User::getAge));
        System.out.println(collect.getMax());
        System.out.println(collect.getMin());
        System.out.println(collect.getSum());
    }

    @Test
    public void test06() {
        List<User> users = Arrays.asList(new User(1, 17, "sky"), new User(1, 19, "张三"), new User(3, 20, "李四"));
	 /* Map<Boolean, List<User>> collect = users.stream().collect(Collectors.partitioningBy(u->u.getAge()>=18));
	 collect.forEach((k,v)->{
		 if(k){
			 System.out.println("成年的："+v);
		 }else{
			 System.out.println("未年的："+v);
		 }
	 });*/
        Map<Boolean, Set<String>> user = users.stream().collect(Collectors.partitioningBy(u -> u.getAge() >= 18, Collectors.mapping(User::getName, Collectors.toSet())));
        user.forEach((k, v) -> {
            if (k) {
                System.out.println("成年的人：" + v);
            } else {
                System.out.println("未年的人：" + v);
            }
        });
    }

    /**
     * collectingAndThen:A类型的Collectors转为B类型的Collectors
     */
    @Test
    public void test07() {
        List<User> users = Arrays.asList(new User(1, 17, "sky"), new User(1, 19, "张三"), new User(3, 20, "李四"));
        //不可变list
        List<User> nlist = users.stream().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
        //nlist.add(new User());
        List<User> nuser = users.stream().collect(collectingAndThen(idCollection(), ArrayList<User>::new));
        System.out.println(nuser);
        //TreeSet<User> collect = users.stream().collect(idCollection());
    }

    public static Collector<User, ?, TreeSet<User>> idCollection() {
        return toCollection(() -> new TreeSet<>(comparingLong(User::getId)));
    }

    private void mian() {
        Stream.of("1", "2", "3", "4", "5").parallel().forEach(d -> {
            System.out.println(findByType(d));
        });
    }

    private String findByType(String type) {
        if ("1".equals(type)) {
            System.out.println("查询第一个");
        } else if ("2".equals(type)) {
            System.out.println("查询第二个");
        } else {
            System.out.println("查询后面的");
        }
        return type;
    }
}
